package com.hehe.library;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hehe.library.activity.BookContent;
import com.hehe.library.activity.MainActivity;

import java.util.List;

/**
 * Created by xiaoz on 2016/6/15.
 */
public class MyRecyclerAdapter extends RecyclerView.Adapter {
    List<MyBook> bookList;
    LayoutInflater layoutInflater;
    int TYPE_VALUE = 2;
    int TYPE_NULL = 1;
    public void setBookList(List<MyBook> bookList) {
        this.bookList = bookList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(layoutInflater == null)
            layoutInflater = LayoutInflater.from(parent.getContext());

        if(viewType == TYPE_VALUE)
        return new MyViewHolder(layoutInflater.inflate(R.layout.recycler_item,parent,false),this);

        return new NullViewHolder(layoutInflater.inflate(R.layout.recycler_item_null,parent,false));
    }

    @Override
    public int getItemViewType(int position) {
            if(bookList == null){
                return TYPE_NULL;
            }else {
                if(bookList.size() == 0){
                    return TYPE_NULL;
                }else {
                    return TYPE_VALUE;
                }
            }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder,int position) {
        if(getItemViewType(position) == TYPE_VALUE) {//若type为有值
            MyViewHolder myViewHolder = (MyViewHolder) viewHolder;
            myViewHolder.updateUi(getItem(position));//更新UI
        }
    }

    @Override
    public int getItemCount() {
        if(bookList == null){
            return 1;
        }else if(bookList.size() == 0){
            return 1;
        }else {
            return bookList.size();
        }
    }

    public MyBook getItem(int position){
        return bookList.get(position);
    }
   public static abstract class BaseViewHolder extends RecyclerView.ViewHolder{

       public  BaseViewHolder(View itemView) {
           super(itemView);
       }
   }
    public static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView mNameTextView;
        TextView mIntroTextView;
        TextView mAuthorTextView;
        TextView mNumTextView;

        public MyViewHolder(View itemView, final MyRecyclerAdapter adapter) {
            super(itemView);
            mNameTextView = (TextView) itemView.findViewById(R.id.name_item);
            mIntroTextView = (TextView) itemView.findViewById(R.id.intro_item);
            mAuthorTextView = (TextView) itemView.findViewById(R.id.author_item);
            mNumTextView = (TextView) itemView.findViewById(R.id.num_item);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(v.getContext(), BookContent.class);
                    String id = adapter.getItem(getAdapterPosition()).getBookid();
                    intent.putExtra("bookid",id);
                    v.getContext().startActivity(intent);
                }
            });
        }
        private void updateUi(MyBook myBook){
            mNameTextView.setText(myBook.getBookname());
            mIntroTextView.setText(myBook.getBookintro());
            mAuthorTextView.setText(myBook.getBookauthor());
            mNumTextView.setText(myBook.getBooknum());
        }
    }
    public static class NullViewHolder extends RecyclerView.ViewHolder {
        TextView mNullTextView;


        public NullViewHolder(View itemView) {
            super(itemView);
            mNullTextView = (TextView) itemView.findViewById(R.id.null_item);

        }
    }

}
